package com.yunshang.shoppingmall.Methods;


import com.yunshang.shoppingmall.entity.Finace.Finace;
import com.yunshang.shoppingmall.entity.FinaceTotal;
import com.yunshang.shoppingmall.service.Finace.FinaceService;
import com.yunshang.shoppingmall.service.Finace.FinaceTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.sql.Timestamp;

@Component
public class FinaceMethods {

    @Autowired
    private FinaceService finaceService1;
    private static FinaceService finaceService;
    @Autowired
    FinaceTotalService finaceTotalService1;
     static FinaceTotalService finaceTotalService;

    @PostConstruct
    public void init() {

        finaceService = finaceService1;
        finaceTotalService=finaceTotalService1;
    }



    public static int insertFinace(int classify, BigDecimal amount, int userId, String remark, int UID, int AID, int finaceType)
    {
        System.out.println("A");
        int n =finaceService.insertFinace(classify,amount, userId, remark,UID, AID,finaceType);
        FinaceTotal finaceTotal = finaceTotalService.selectFinaceTotal(userId,classify+"");
        if(finaceTotal!=null && finaceTotal.getBalance()!=null)
        {
            System.out.println("B");
            FinaceTotal finaceTotal1 = new FinaceTotal();
            finaceTotal1.setfId(finaceTotal.getfId());
            finaceTotal1.setUserId(userId);
            finaceTotal1.setAmount(Double.parseDouble(amount.toString()));
            finaceTotal1.setBalance(Double.parseDouble(amount.toString())+finaceTotal.getBalance());
            finaceTotal1.setOpratorId(AID);
            finaceTotal1.setOpratorUId(UID);
            finaceTotal1.setChanggeTime(new Timestamp(System.currentTimeMillis()));
            int firstRow = finaceTotalService.updateByPrimaryKeySelective(finaceTotal1);
            System.out.println("BB"+firstRow);
        }
        else
        {
            System.out.println("C");
            FinaceTotal finaceTotal1 = new FinaceTotal();
            finaceTotal1.setUserId(userId);
            finaceTotal1.setAmount(Double.parseDouble(amount.toString()));
            finaceTotal1.setBalance(Double.parseDouble(amount.toString()));
            finaceTotal1.setfType(classify+"");
            finaceTotal1.setChanggeTime(new Timestamp(System.currentTimeMillis()));
            finaceTotal1.setOpratorId(AID);
            finaceTotal1.setOpratorUId(UID);
            int firstRow = finaceTotalService.insertSelective(finaceTotal1);
            System.out.println("CC"+firstRow);
        }
        return n;
    }

    //后端
    public static int insertFinaceA(int classify, BigDecimal amount, int userId, String remark, int AID, int finaceType){
        return insertFinace(classify, amount, userId, remark, 0, AID, finaceType);
    }

    /**
     * 添加财务信息
     * @param classify     财务种类
     * @param amount       财务金额
     * @param userId       用户ID
     * @param remark       备注
     * @param UID          操作UID 前端
     * @paramAID            操作Aid 后端
     * @param finaceType   财务类型
     * @return             插入条数
     */
    //前端
    public static int insertFinaceU(int classify, BigDecimal amount, int userId, String remark, int UID, int finaceType){
        return insertFinace(classify, amount, userId, remark, UID, 0, finaceType);
    }

    /**
     * 获取用户余额
     * @param userId    用户Id
     * @param classify  财务种类
     * @return
     */
    public static BigDecimal getUserBalance(int userId,int classify){
        return finaceService.selectByPrimaryUserId(classify,userId);
    }

    //查询账户所有货币余额
    public static Finace selectFinaceAll(Integer userId) {
        return finaceService.selectFinaceAll(userId);
    }

    //查询用户财务余额
    public static FinaceTotal selectFinaceBalace(FinaceTotal finaceTotal){
        return finaceTotalService.selectFinaceBalace(finaceTotal);
    }

}